package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {

    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    // 分页查询所有护理项目
    @Override
    public PageResponse<NursingProjectVo> selectByPage(Integer pageNum, Integer pageSize, String name, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingProjectVo> page = nursingProjectMapper.selectByPage(pageNum, pageSize, name, status);

        return PageResponse.of(page, NursingProjectVo.class);
    }

    // 新增护理项目
    @Override
    public void saveProject(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = new NursingProject();
        BeanUtil.copyProperties(nursingProjectDto, nursingProject);
        int count = nursingProjectMapper.countByName(nursingProject);
        if (count > 0) {
            throw new BaseException(BasicEnum.NURSING_PROJECT_INSERT_FAIL_DUPLICATION_NAME);
        }
        int number = nursingProjectDto.getOrderNo();
        if (number > 1000) {
            throw new RuntimeException("护理项目编号不能大于1000");
        }
        int i = nursingProjectMapper.countByOrderNo(number);
        if (i != 0) {
            throw new RuntimeException("护理项目编号不能重复");
        }
        nursingProjectMapper.insertNursingProject(nursingProject);
    }


    // 根据id修改护理项目
    // 根据id查询
    @Override
    public NursingProjectVo getById(Long id) {
        int count = nursingProjectMapper.countById(id);
        if (count == 0) {
            throw new BaseException(BasicEnum.NURSING_PROJECT_SELECT_ID_FAIL);
        }


        return nursingProjectMapper.readNursingProjectById(id);
    }

    // 根据id修改
    @Override
    public void updateById(NursingProjectDto nursingProjectDto) {
        int count = nursingProjectMapper.countById(nursingProjectDto.getId());
        if (count == 0) {
            throw new BaseException(BasicEnum.NURSING_PROJECT_SELECT_ID_FAIL);
        }
        NursingProject nursingProject = BeanUtil.copyProperties(nursingProjectDto, NursingProject.class);
        nursingProject.setUpdateTime(LocalDateTime.now());
        nursingProjectMapper.updateNursingProject(nursingProject);
    }

    // 根据id删除护理项目
    @Override
    public int removeById(Long id) {
        int count = nursingProjectMapper.countById(id);
        if (count == 0) {
            throw new BaseException(BasicEnum.NURSING_PROJECT_SELECT_ID_FAIL);
        }
        // 判断计划是否是启用状态，如果是则不能删除
        int status = nursingProjectMapper.getStatus(id);
        if (status == 1) {
            throw new BaseException(BasicEnum.NURSING_PROJECT_DELETE_FAIL);
        }
        return nursingProjectMapper.deleteNursingProjectById(id);
    }

    // 启用/禁用
    @Override
    public int isEnable(Long id, Integer status) {
        int count = nursingProjectMapper.countById(id);
        if (count == 0) {
            throw new BaseException(BasicEnum.NURSING_PROJECT_SELECT_ID_FAIL);
        }
        nursingProjectMapper.alterStatus(id, status);
        return count;
    }

    // 查询所有项目
    @Override
    public List<NursingProjectVo> selectAll() {
        return nursingProjectMapper.selectAll();
    }
}
